package com.safeluck.scannerqcr.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.safeluck.scannerqcr.R;
import com.safeluck.scannerqcr.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * ScannerQCR
 * Created by lzw on 2017/12/7. 13:32:32
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class FirstPagerAdapter extends RecyclerView.Adapter<FirstPagerAdapter.FirstPagerHodler> {

    private LayoutInflater mInflater;
    private List<String> stringList = new ArrayList<>();
    private OnItemClickListener mListener;


    public FirstPagerAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public void setDatas(List<String> strs){
        stringList.clear();
        stringList.addAll(strs);
        notifyDataSetChanged();
    }

    @Override
    public FirstPagerHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.first_pager_item,parent,false);
        final FirstPagerHodler firstPagerHodler = new FirstPagerHodler(view);
        firstPagerHodler.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.onItemClick(firstPagerHodler.getAdapterPosition(),firstPagerHodler.itemView,
                            firstPagerHodler);
                }
            }
        });
        return firstPagerHodler;
    }

    @Override
    public void onBindViewHolder(FirstPagerHodler holder, int position) {
        String str = stringList.get(position);
        holder.tv.setText(str);
    }

    @Override
    public int getItemCount() {
        return stringList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mListener = onItemClickListener;
    }

    public String getItem(int position) {
        return stringList.get(position);
    }

    class FirstPagerHodler extends RecyclerView.ViewHolder{
        private TextView tv;
        public FirstPagerHodler(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_conn);
        }
    }
}
